package com.anrongtec.zcpt.ui.activity;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.IntentCompat;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.utils.DialogUtil;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.clv_setting_photo)
    CircleImageView clvSettingPhoto;
    @BindView(R.id.ll_setting_photo)
    LinearLayout llSettingPhoto;
    @BindView(R.id.tv_setting_name)
    TextView tvSettingName;
    @BindView(R.id.ll_setting_name)
    LinearLayout llSettingName;
    @BindView(R.id.tv_setting_sex)
    TextView tvSettingSex;
    @BindView(R.id.ll_setting_sex)
    LinearLayout llSettingSex;
    @BindView(R.id.tv_setting_class)
    TextView tvSettingClass;
    @BindView(R.id.ll_setting_class)
    LinearLayout llSettingClass;
    @BindView(R.id.ll_setting_cache)
    LinearLayout llSettingCache;
    @BindView(R.id.ll_setting_about_me)
    LinearLayout llSettingAboutMe;
    @BindView(R.id.ll_setting_password)
    LinearLayout llSettingPassword;
    @BindView(R.id.btn_quit)
    Button btnQuit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initTopBar(this,true,"设置","");
    }

    @OnClick({R.id.ll_setting_photo,R.id.ll_setting_cache, R.id.ll_setting_about_me, R.id.ll_setting_password, R.id.btn_quit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_setting_photo://更改照片
                break;
            case R.id.ll_setting_cache://清除缓存
                setCleanCache();
                break;
            case R.id.ll_setting_about_me://关于页
                startActivity(new Intent(this,AboutUsActivity.class));
                break;
            case R.id.ll_setting_password://修改密码页
                startActivity(new Intent(this,AmendPasswordActivity.class));
                break;
            case R.id.btn_quit://退出登录
                logout();
                break;
        }
    }



    /**
     * 清空缓存点击事件
     */
    private void setCleanCache() {
        DialogUtil.createAlertDialog(this, "清除缓存", 0, "是否清除缓存？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //TODO 清除缓存
                Glide.get(getApplicationContext()).clearMemory();
            }
        });
    }

    /**
     * 退出登录
     */
    private void logout() {
        DialogUtil.createAlertDialog(this, "退出登录", 0, "是否退出当前应用？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//
//                String json = GsonUtil.toLogoutJson();
//                HttpUtil.post(json, new StringCallback() {
//                    @Override
//                    public void onResponse(boolean arg0, String arg1, Request arg2, Response arg3) {
//                        JsonDataRestult<Object> result = GsonUtil.decode(arg1, new TypeToken<JsonDataRestult<Object>>(){}.getType());
//                        if (URLConstants.RetCode.SUCCESS.equals(result.getRetCode())) {
//
//                        }
//                    }
//                });

                //TODO 上报登出
//				String accountId = PreferenceUtils.getInstance(SettingActivity.this).getString(PreferenceKey.ACCOUNT_ID);
//				WLData.getInstance().reportLogout(accountId, Events.LOGOUT);

//                退出到登录页面
				Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
				ComponentName cn = intent.getComponent();
				Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
				startActivity(mainIntent);
                showToastCustom("已退出", ApiConfig.SUCCESS);

                //解绑统一认证服务
//				UAManager.getInstance().unbindService();

                //完全退出
//				android.os.Process.killProcess(android.os.Process.myPid());
//				System.exit(1);
//                UAManager.getInstance().authLogout("");
//                intent = new Intent(Constant.action.EXIT);
//                sendBroadcast(intent);
                finish();
            }
        });

    }

}
